﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using AEM.Common;
using AEM.Data;
using AEM.Data.Enum;

namespace AEM.Controls
{
    /// <summary>
    /// HallCallLog.xaml 的交互逻辑
    /// </summary>
    public partial class HallCallLog : UserControl
    {
        public HallCallLog()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WaitingRatio_Loaded);
            this.btn_select.Click += new RoutedEventHandler(btn_select_Click);
            this.datefrom.ValueChanged += new EventHandler(datefrom_SelectedDateChanged);
            this.dateto.ValueChanged += new EventHandler(dateto_SelectedDateChanged);
            this.btn_reset.Click += new RoutedEventHandler(btn_reset_Click);
            this.liftNum.SelectionChanged += new SelectionChangedEventHandler(liftNum_SelectionChanged);
        }

        void liftNum_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Data.Model.LiftInfo r = (Data.Model.LiftInfo)this.liftNum.SelectedItem;
            if (null == r) return;

            this.liftLandings.Visibility = Visibility.Visible;
            List<KeyValuePair<string, int>> flist = new List<KeyValuePair<string, int>>();
            string[] floors = r.LiftActualFloor.Split(',');
            int len = floors.Length;
            for (int i = 1; i <= len; i++)
            {
                flist.Add(new KeyValuePair<string, int>(floors[i - 1], i));
            }
            this.liftLandings.ItemsSource = flist;
            this.liftLandings.Items.Refresh();
        }

        void btn_reset_Click(object sender, RoutedEventArgs e)
        {
            this.liftNum.SelectedItem = null;
            this.liftDirection.SelectedItem = null;
            this.liftLandings.SelectedItem = null;
            this.datefrom.Value = DateTime.Now;
            this.dateto.Value = DateTime.Now;
            this.liftLandings.Visibility = Visibility.Collapsed;
        }

        void WaitingRatio_Loaded(object sender, RoutedEventArgs e)
        {
            if (null != Config.LiftInftList)
                this.liftNum.ItemsSource = from Data.Model.LiftInfo r in Config.LiftInftList
                                           where r.LiftType == (int)LiftType.电梯
                                           select r;
            this.liftNum.Items.Refresh();
            //this.liftNum.SelectedIndex = 0;

            DateTime s = DataLogHelper.GetEarliestTime();
            if (null != s)
            {
                this.datefrom.DisplayDateStart = s;
                this.dateto.DisplayDateStart = s;
            }
            else
            {
                this.datefrom.DisplayDateStart = DateTime.Now;
            }
            this.datefrom.DisplayDateEnd = DateTime.Now;
            this.dateto.DisplayDateEnd = DateTime.Now;

            List<KeyValuePair<string, int>> dlist = new List<KeyValuePair<string, int>>();
            dlist.Add(new KeyValuePair<string, int>(this.TryFindResource("UpCall") as string, (int)CallType.Up));
            dlist.Add(new KeyValuePair<string, int>(this.TryFindResource("DownCall") as string, (int)CallType.Down));
            this.liftDirection.ItemsSource = dlist;
            this.liftDirection.Items.Refresh();
            //if (null != Config.LiftInftList)
            //{
            //    var f = (from Data.Model.LiftInfo r in Config.LiftInftList
            //             where r.LiftType == (int)LiftType.电梯
            //             select r.LiftActualFloor).Max();

            //    List<KeyValuePair<string, int>> flist = new List<KeyValuePair<string, int>>();
            //    int len = f.Split(',').Length;
            //    for (int i = 1; i <= len; i++)
            //    {
            //        flist.Add(new KeyValuePair<string, int>(i.ToString(), i));
            //    }
            //    this.liftLandings.ItemsSource = flist;
            //    this.liftLandings.Items.Refresh();
            //}
        }

        void dateto_SelectedDateChanged(object sender, EventArgs e)
        {
            this.datefrom.DisplayDateEnd = this.dateto.Value;
        }

        void datefrom_SelectedDateChanged(object sender, EventArgs e)
        {
            this.dateto.DisplayDateStart = this.datefrom.Value;
        }

        void btn_select_Click(object sender, RoutedEventArgs e)
        {
            this.listHallCallLog.ItemsSource = null; 
            this.listHallCallLog.Items.Refresh();
            this.btn_reset.IsEnabled = false;
            this.msg.Text = "";
            if (null == this.datefrom.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateFrom") as string);
                this.btn_reset.IsEnabled = true; return;
            }
            if (null == this.dateto.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateTo") as string);
                this.btn_reset.IsEnabled = true; return;
            }
            DateTime s = this.datefrom.Value.Value;
            DateTime t = this.dateto.Value.Value;
            int d = -1;
            int l = -1;
            if (null != this.liftDirection.SelectedValue)
                d = (int)this.liftDirection.SelectedValue;
            if (null != this.liftLandings.SelectedValue)
                l = (int)this.liftLandings.SelectedValue;

            this.msg.Text = this.TryFindResource("Searching") as string;
            if (null != this.liftNum.SelectedItem)
            {
                Data.Model.LiftInfo r = (Data.Model.LiftInfo)this.liftNum.SelectedItem;

                this.listHallCallLog.ItemsSource = CreateData(r, s, t, d, l);
            }
            else
            {
                //var f = (from Data.Model.LiftInfo r in AEM.Data.BLL.Actions.LiftInfo.List("")
                //         where r.LiftType == (int)LiftType.电梯
                //         select r.LiftActualFloor).Max();
                //int len = f.Split(',').Length;

                this.listHallCallLog.ItemsSource = CreateData(null, s, t, d, l);
            }

            this.listHallCallLog.Items.Refresh();
            this.listHallCallLog.SelectedIndex = 0;
            this.btn_reset.IsEnabled = true;
            this.msg.Text = this.TryFindResource("SearchOK") as string;
        }

        private IEnumerable<HallCallLogItem> CreateData(Data.Model.LiftInfo r, DateTime s, DateTime t, int d, int l)
        {
            List<Data.DataLog.TableHallCallLogRow> logList = new List<DataLog.TableHallCallLogRow>();
            if (null != r)
            {
                logList = DataLogHelper.GetHallCallLog(r, s, t);
            }
            else
            {
                if (null != Config.LiftInftList)
                {
                    List<StatusRatioItem> list = new List<StatusRatioItem>();
                    foreach (Data.Model.LiftInfo lt in Config.LiftInftList)
                    {
                        logList.AddRange(DataLogHelper.GetHallCallLog(lt, s, t).ToArray());
                    }
                }
            }
            if (null == logList) return null;
            if (-1 != d)
            {
                if (-1 != l)
                {
                    logList = (from DataLog.TableHallCallLogRow i in logList
                               where i.Floor == l && i.CallType == d
                               select i).ToList();

                }
                else
                {
                    logList = (from DataLog.TableHallCallLogRow i in logList
                               where i.CallType == d
                               select i).ToList();
                }

            }
            else
            {
                if (-1 != l)
                {
                    logList = (from DataLog.TableHallCallLogRow i in logList
                               where i.Floor == l
                               select i).ToList();

                }
                //else
                //{
                //    logList = (from DataLog.TableHallCallLogRow i in logList
                //               select i).ToList();
                //}
            }

            int index = 1;

            if (null != logList && logList.Count > 0)
            {
                try
                {
                    string uc = this.TryFindResource("UpCall") as string;//"上呼" 
                    string dc = this.TryFindResource("DownCall") as string;//"下呼"
                    if (null == Config.LiftInftList) return null;
                    List<HallCallLogItem> hlist = (from DataLog.TableHallCallLogRow i in logList
                                                   join Data.Model.LiftInfo lr in Config.LiftInftList
                                                   on i.LiftNum equals lr.LiftNum
                                                   orderby i.LiftNum, i.StartTime descending
                                                   select new HallCallLogItem
                                                   {
                                                       RowNum = index++,
                                                       LiftInfo = lr,
                                                       ShortName = lr.ShortName,
                                                       CallType = i.CallType == 0 ? uc: dc,
                                                       Landings = GetActualFloor(lr,i.Floor) ,
                                                       CallTime = i.StartTime,
                                                       EndTime = i.EndTime
                                                   }).ToList();
                    return hlist;
                }
                catch { this.msg.Text = "数据转换失败，请联系管理员！"; }
            }
            return null;
        }

        private string GetActualFloor(Data.Model.LiftInfo lift, int floor)
        {
            string[] arr = lift.LiftActualFloor.Split(',');
            if (arr.Length >= floor) return arr[floor - 1];
            return floor.ToString();
        }
    }

    public class HallCallLogItem
    {
        public int RowNum { get; set; }
        public Data.Model.LiftInfo LiftInfo { get; set; }
        public string ShortName { get; set; }
        public string CallType { get; set; }
        public string Landings { get; set; }
        public DateTime CallTime { get; set; }
        public DateTime EndTime { get; set; }
    }
}
